<template>
    <div>
        <h2>修改积分页面</h2>
        <table class="table table-bordered">
            <tr>
                <td>积分</td>
                <td>
                    <input type="text" v-model="state.Intergral"/>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="增加" @click="cocal(num)"/>
                    <input type="button" value="减少" @click="cocal(-num)">
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="保存"  @click="save"/>
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { reactive,toRefs,onMounted } from 'vue';
import { useRouter, useRoute} from 'vue-router';
const router= useRouter()
const route=useRoute()

const data=reactive({
    state:{},
    Intergral:'',
    num:100
})
const {state,Intergral,num}=toRefs(data)

onMounted(()=>{
    UpdIntergral();
})
//反填积分
const UpdIntergral=()=>{
    axios({
        url:'/api/High/UpdIntergral',
        method:'get',
        params:{
            Id:route.query.Id
        }
    })
    .then(res=>{
        console.log(res);
        state.value=res.data.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

const cocal=(num:any)=>{
    if(state.value.Intergral<=0&&num<0)
    {
        return;
    }
    state.value.Intergral=Number(state.value.Intergral)+num;
}
//let obj是声明变量
const save=()=>{
    if(state.value.Intergral==='')
    {
        alert('积分不能为空');
        return;
    }
    if(isNaN(Number(state.value.Intergral)))
    {
        alert('积分只能是数字');
        return;
    }
    let obj={
        Id:route.query.Id,
        Intergral:state.value.Intergral
    }
    axios({
        url:'/api/High/UpdIntergral',
        method:'put',
        params:obj
    })
    .then(res=>{
        console.log(res);
        if(res.data.data>0)
        {
            alert('保存成功');
            router.push('/ShowMember');
        }
        else
        {
            alert('保存失败');
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

</script>
<style lang="scss" scoped>

</style>